package com.dalongxin.work.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

	/**
	 * 排除为空赋值的字符串
	 * @param content 需要排除的字符串
	 * @param replace 用来替换原来的null
	 * */
	public static String excludeNull(String content, String replace){
		if(TextUtils.isEmpty(content)){
			if(TextUtils.isEmpty(replace)){
				return "";
			}else {
				return replace;
			}
		}
		return content;
	}

	/**
	 * 排除为空赋值的字符串
	 * @param content 需要排除的字符串
	 * */
	public static String excludeNull(String content){
		return excludeNull(content,"");
	}

	public static boolean isEmpty(String s) {
		if(s == null)
			return true;
		if("".equals(s.trim()))
			return true;
		return false;
	}
	
	public static boolean isEmptyOrNull(String s) {
		if(s == null || "null".equals(s.trim()))
			return true;
		if("".equals(s.trim()))
			return true;
		return false;
	}
	
	public static String strsub(String str, int max, boolean flag){
		if(isEmptyOrNull(str))
			return "";
		if(max >= str.length())
			return str;
		return str.substring(0,max)+(flag?"...":"");
	}
	
	/**
	 * @param mobiles
	 * @return [0-9]{5,9}
	 */
	public static boolean isMobileNO(String mobiles) {
		boolean flag = false;
		try {
//			Pattern p = Pattern
//					.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
			Pattern p = Pattern.compile("^0?1\\d{10}$");
			Matcher m = p.matcher(mobiles);
			flag = m.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	public static String format(String date, String format, String exportFormat) {
		if(date == null) return "";
		Date d = null;
		SimpleDateFormat sdf =  new SimpleDateFormat(format);
		try {
			d = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}
		if(d == null) {
			return "";
		}
		SimpleDateFormat exportSdf =  new SimpleDateFormat(exportFormat);
		return exportSdf.format(d);
	}
	
	public static boolean isDate(String date, String format) {
		if(date == null)return false;
		Date d = null;
		SimpleDateFormat sdf =  new SimpleDateFormat(format);
		try {
			d = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
		if(d == null) {
			return false;
		}
		return true;
	}
	
	public static String doubleTrans(double d) {
		if (Math.round(d) - d == 0) {
			return String.valueOf((long) d);
		}
		return String.valueOf(d);
	}
	
	public static boolean isNumeric(String str){
		if(str == null || "".equals(str))return false;
	   Pattern pattern = Pattern.compile("[0-9]*");
	   Matcher isNum = pattern.matcher(str);
	   if( !isNum.matches() ){
	       return false; 
	   } 
	   return true; 
	}

	public static long toLong(String str){
		if(isNumeric(str)){
			return Long.valueOf(str);
		}
		return 0;
	}
	
	public static String getFileNameByUrl(String url) {
		if(url != null && url.contains("/") && url.contains(".")) {
			String fileName = url.substring(url.lastIndexOf("/") + 1);
			return fileName.substring(0, fileName.indexOf("."));
		}
		return "";
	}

	public static String listToString(List<String> letters, String separator) {
		if(letters == null || letters.size() == 0) return "";
		StringBuilder strBuilder = new StringBuilder();
		for(String letter : letters){
			strBuilder.append(letter);
			strBuilder.append(separator);
		}
		String strLetter = strBuilder.toString();
		//Remove last comma
		strLetter = strLetter.substring(0, strLetter.length() - separator.length());
		return strLetter;
	}
}
